package Algorithm.Greedy;

import org.junit.Test;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Random;

public class ReflectionTest {

    @Test
    public void test1() throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException, NoSuchFieldException, ClassNotFoundException, IOException {
        Class clazz = Person.class;
        Person p1 = (Person) clazz.newInstance();
        System.out.println(p1);
    }

    @Test
    public void test2(){
        int num = new Random().nextInt(3);
        String classPath = "";
        switch (num){
            case 0:
                classPath = "java.lang.Object";
                break;
            case 1:
                classPath = "java.util.Date";
                break;
            case 2:
                classPath = "Algorithm.Greedy.Person";
                break;
        }
        try {
            Object obj = getInstance(classPath);
            System.out.println(obj);
        } catch (Exception e){
            e.printStackTrace();
        }
    }

    private Object getInstance(String classPath) throws IllegalAccessException, InstantiationException, ClassNotFoundException {
        Class clazz = Class.forName(classPath);
        Object obj = clazz.newInstance();
        return obj;
    }


}
